package cn.iocoder.yudao.module.system.controller.admin.gugu;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.module.system.controller.admin.gugu.vo.MajorRecommendItemVO;
import cn.iocoder.yudao.module.system.controller.admin.gugu.vo.MajorRecommendReqVO;
import cn.iocoder.yudao.module.system.controller.admin.gugu.vo.MajorRecommendRespVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

/**
 * 智能推荐专业简化控制器（用于测试）
 *
 * @author 芋道源码
 */
@Tag(name = "管理后台 - 智能推荐专业简化版")
@RestController
@RequestMapping("/system/major-recommendation-simple")
@Validated
@Slf4j
public class MajorRecommendationSimpleController {

    @PostMapping("/recommend")
    @Operation(summary = "智能推荐专业（简化版）")
    public CommonResult<MajorRecommendRespVO> recommendMajors(@Valid @RequestBody MajorRecommendReqVO reqVO) {
        log.info("智能推荐专业请求（简化版）：{}", reqVO);
        
        try {
            // 创建模拟数据
            MajorRecommendRespVO respVO = createMockResponse(reqVO);
            return success(respVO);
        } catch (Exception e) {
            log.error("智能推荐专业失败", e);
            return CommonResult.error(500, "推荐服务异常：" + e.getMessage());
        }
    }

    /**
     * 创建模拟响应数据
     */
    private MajorRecommendRespVO createMockResponse(MajorRecommendReqVO reqVO) {
        MajorRecommendRespVO respVO = new MajorRecommendRespVO();
        
        // 创建模拟推荐专业列表
        List<MajorRecommendItemVO> majorList = new ArrayList<>();
        
        // 冲刺专业
        MajorRecommendItemVO chongItem = createMockMajorItem(
                1L, "计算机科学与技术", "080901", "uuid-001", "南京大学", 
                "江苏", "南京", "工学", "计算机类", 45, "CHONG", "冲刺");
        majorList.add(chongItem);
        
        // 稳妥专业
        MajorRecommendItemVO wentuoItem = createMockMajorItem(
                2L, "软件工程", "080902", "uuid-002", "东南大学", 
                "江苏", "南京", "工学", "计算机类", 75, "WENTUO", "稳妥");
        majorList.add(wentuoItem);
        
        // 保底专业
        MajorRecommendItemVO baodiItem = createMockMajorItem(
                3L, "信息管理与信息系统", "120102", "uuid-003", "南京理工大学", 
                "江苏", "南京", "管理学", "管理科学与工程类", 90, "BAODI", "保底");
        majorList.add(baodiItem);
        
        // 设置分页信息
        respVO.setTotal(3L);
        respVO.setList(majorList);
        respVO.setPageNum(reqVO.getPageNum());
        respVO.setPageSize(reqVO.getPageSize());
        respVO.setSize(majorList.size());
        respVO.setStartRow(1);
        respVO.setEndRow(3);
        respVO.setPages(1);
        respVO.setPrePage(0);
        respVO.setNextPage(0);
        respVO.setIsFirstPage(true);
        respVO.setIsLastPage(true);
        respVO.setHasPreviousPage(false);
        respVO.setHasNextPage(false);
        respVO.setNavigatePages(8);
        respVO.setNavigatepageNums(new int[]{1});
        respVO.setNavigateFirstPage(1);
        respVO.setNavigateLastPage(1);
        
        // 设置汇总信息
        MajorRecommendRespVO.SummaryVO summary = new MajorRecommendRespVO.SummaryVO();
        summary.setUserScore(reqVO.getScore());
        summary.setUserRank(reqVO.getRanks() != null ? reqVO.getRanks() : 15000);
        summary.setProvince(reqVO.getProvince());
        summary.setExamType(reqVO.getClassify() + "类");
        summary.setSubjectCombination(reqVO.getSubjects().replace(",", "+"));
        summary.setChongCount(1);
        summary.setWentuoCount(1);
        summary.setBaodiCount(1);
        summary.setAnalysisText(String.format("基于您的分数%d分，位次%d名，为您推荐3个专业", 
                reqVO.getScore(), summary.getUserRank()));
        respVO.setSummary(summary);
        
        // 设置统计信息
        MajorRecommendRespVO.StatisticsVO statistics = new MajorRecommendRespVO.StatisticsVO();
        
        // 学科门类分布
        List<MajorRecommendRespVO.StatisticsVO.CategoryStatVO> categoryStats = new ArrayList<>();
        MajorRecommendRespVO.StatisticsVO.CategoryStatVO categoryStat1 = new MajorRecommendRespVO.StatisticsVO.CategoryStatVO();
        categoryStat1.setCategory("工学");
        categoryStat1.setCount(2);
        categoryStat1.setPercentage(66.7);
        categoryStats.add(categoryStat1);
        
        MajorRecommendRespVO.StatisticsVO.CategoryStatVO categoryStat2 = new MajorRecommendRespVO.StatisticsVO.CategoryStatVO();
        categoryStat2.setCategory("管理学");
        categoryStat2.setCount(1);
        categoryStat2.setPercentage(33.3);
        categoryStats.add(categoryStat2);
        statistics.setCategoryStats(categoryStats);
        
        // 地域分布
        List<MajorRecommendRespVO.StatisticsVO.RegionStatVO> regionStats = new ArrayList<>();
        MajorRecommendRespVO.StatisticsVO.RegionStatVO regionStat = new MajorRecommendRespVO.StatisticsVO.RegionStatVO();
        regionStat.setRegion("江苏");
        regionStat.setCount(3);
        regionStat.setPercentage(100.0);
        regionStats.add(regionStat);
        statistics.setRegionStats(regionStats);
        
        // 学校类型分布
        List<MajorRecommendRespVO.StatisticsVO.SchoolTypeStatVO> schoolTypeStats = new ArrayList<>();
        MajorRecommendRespVO.StatisticsVO.SchoolTypeStatVO schoolTypeStat = new MajorRecommendRespVO.StatisticsVO.SchoolTypeStatVO();
        schoolTypeStat.setSchoolType("985/211");
        schoolTypeStat.setCount(3);
        schoolTypeStat.setPercentage(100.0);
        schoolTypeStats.add(schoolTypeStat);
        statistics.setSchoolTypeStats(schoolTypeStats);
        
        // 概率分布
        List<MajorRecommendRespVO.StatisticsVO.ProbabilityStatVO> probabilityStats = new ArrayList<>();
        MajorRecommendRespVO.StatisticsVO.ProbabilityStatVO probabilityStat1 = new MajorRecommendRespVO.StatisticsVO.ProbabilityStatVO();
        probabilityStat1.setProbabilityRange("80-100%");
        probabilityStat1.setCount(2);
        probabilityStat1.setPercentage(66.7);
        probabilityStats.add(probabilityStat1);
        
        MajorRecommendRespVO.StatisticsVO.ProbabilityStatVO probabilityStat2 = new MajorRecommendRespVO.StatisticsVO.ProbabilityStatVO();
        probabilityStat2.setProbabilityRange("40-60%");
        probabilityStat2.setCount(1);
        probabilityStat2.setPercentage(33.3);
        probabilityStats.add(probabilityStat2);
        statistics.setProbabilityStats(probabilityStats);
        
        respVO.setStatistics(statistics);
        
        return respVO;
    }

    /**
     * 创建模拟专业推荐项
     */
    private MajorRecommendItemVO createMockMajorItem(Long majorId, String majorName, String majorCode,
                                                   String schoolUuid, String schoolName, String schoolProvince,
                                                   String schoolCity, String disciplinaryCategory, 
                                                   String disciplinarySubCategory, Integer enrollProbability,
                                                   String type, String typeDesc) {
        MajorRecommendItemVO item = new MajorRecommendItemVO();
        
        // 基本信息
        item.setMajorId(majorId);
        item.setMajorName(majorName);
        item.setMajorCode(majorCode);
        item.setSchoolUuid(schoolUuid);
        item.setSchoolName(schoolName);
        item.setSchoolProvince(schoolProvince);
        item.setSchoolCity(schoolCity);
        item.setDisciplinaryCategory(disciplinaryCategory);
        item.setDisciplinarySubCategory(disciplinarySubCategory);
        item.setEnrollProbability(enrollProbability);
        item.setType(type);
        item.setTypeDesc(typeDesc);
        
        // 推荐理由
        item.setReason(String.format("%s推荐：%s的%s专业录取概率为%d%%，%s", 
                typeDesc, schoolName, majorName, enrollProbability,
                type.equals("CHONG") ? "虽然有一定挑战性，但值得尝试。" :
                type.equals("WENTUO") ? "录取把握较大，是理想的选择。" :
                "录取概率很高，可作为保底选择。"));
        
        // 分数信息
        MajorRecommendItemVO.ScoreInfoVO scoreInfo = new MajorRecommendItemVO.ScoreInfoVO();
        scoreInfo.setLowestScore(580 - (90 - enrollProbability) / 2);
        scoreInfo.setAverageScore(scoreInfo.getLowestScore() + 5);
        scoreInfo.setHighestScore(scoreInfo.getAverageScore() + 5);
        scoreInfo.setLowestRank(15000 + (90 - enrollProbability) * 100);
        scoreInfo.setAverageRank(scoreInfo.getLowestRank() - 500);
        scoreInfo.setYear(2023);
        item.setScoreInfo(scoreInfo);
        
        // 招生计划信息
        MajorRecommendItemVO.EnrollmentInfoVO enrollmentInfo = new MajorRecommendItemVO.EnrollmentInfoVO();
        enrollmentInfo.setPlanCount(50);
        enrollmentInfo.setSubjectRequirement("物理+化学");
        enrollmentInfo.setDuration("4年");
        enrollmentInfo.setTuition("5000");
        item.setEnrollmentInfo(enrollmentInfo);
        
        // 专业详情
        MajorRecommendItemVO.MajorDetailVO majorDetail = new MajorRecommendItemVO.MajorDetailVO();
        majorDetail.setIntroduction(majorName + "是一个很好的专业，就业前景广阔。");
        majorDetail.setMainCourses(Arrays.asList("高等数学", "数据结构", "算法设计"));
        majorDetail.setCareerDirection("软件开发、系统分析、技术管理等");
        majorDetail.setGraduateScale("大型");
        majorDetail.setMaleFemaleRatio("7:3");
        majorDetail.setRecommendSchools(Arrays.asList("清华大学", "北京大学", "中科院"));
        item.setMajorDetail(majorDetail);
        
        // 标签
        item.setSchoolTags(Arrays.asList("985", "211", "双一流"));
        item.setMajorTags(Arrays.asList("热门专业", "就业前景好"));
        
        // 排序权重
        item.setSortWeight(enrollProbability + (type.equals("BAODI") ? 300 : type.equals("WENTUO") ? 200 : 100));
        
        return item;
    }

    @GetMapping("/health")
    @Operation(summary = "健康检查")
    public CommonResult<String> health() {
        return success("智能推荐专业接口运行正常");
    }
}
